package com.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionUtil {
	/***
	 *  强调Mybatis的获取和基本的操作
	 */
	private SqlSessionFactory build;
	private SqlSession session=null;

	public SessionUtil() { //使用构造方法 获取session对象

		InputStream is;
		try {
			is = Resources.getResourceAsStream("mybatis_config.xml");

			build = new SqlSessionFactoryBuilder().build(is);

			session = build.openSession();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void destory(){
		if(session!=null){
			session.close();
		}
	}
	//编写一个获取SqlSession的方法
	public SqlSession getSession(){
		if(session!=null){
			return session;
		}
		return null;


	}


}
